package org.njyinghuo.sdk.bytedance.client.douyin;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.njyinghuo.sdk.bytedance.conf.Config;
import org.njyinghuo.sdk.bytedance.exception.DouYinException;

/**
 * @description: 刷新refresh_token
 * @author: Hupengpeng
 * @time: 2021/7/3 10:38
 */
public class OauthRenewRefreshToken {
    private String client_key;
    private String refresh_token;
    private String response;
    private OauthRenewRefreshToken(){}
    public String getClient_key() {
        return client_key;
    }

    public OauthRenewRefreshToken setClient_key(String client_key) {
        this.client_key = client_key;
        return this;
    }

    public String getRefresh_token() {
        return refresh_token;
    }

    public OauthRenewRefreshToken setRefresh_token(String refresh_token) {
        this.refresh_token = refresh_token;
        return this;
    }

    public String getResponse() {
        return response;
    }

    public void setResponse(String response) {
        this.response = response;
    }

    protected static OauthRenewRefreshToken New() {
        return new OauthRenewRefreshToken();
    }

    public OauthRenewRefreshToken execute() {
        String url = StrUtil.format("{}{}?client_key={}&refresh_token={}",
                Config.API_HTTP_SCHEME + Config.API_HOST,
                Config.API_OAUTH_RENEW_REFRESH_TOKEN,
                getClient_key(),
                getRefresh_token());
        String rs = HttpUtil.get(url);
        response = rs;
        return this;
    }

    public OauthRenewRefreshTokenResponse convert() {
        JSONObject obj = JSONUtil.parseObj(response);
        if ("success".equalsIgnoreCase(obj.getStr("message"))) {
            return JSONUtil.toBean(obj.getJSONObject("data"), OauthRenewRefreshTokenResponse.class);
        }
        throw new DouYinException(response);
    }

    public class OauthRenewRefreshTokenResponse {
        private String captcha;
        private String desc_url;
        private String description;
        private int error_code;
        private int expires_in;
        private String refresh_token;

        public String getCaptcha() {
            return captcha;
        }

        public void setCaptcha(String captcha) {
            this.captcha = captcha;
        }

        public String getDesc_url() {
            return desc_url;
        }

        public void setDesc_url(String desc_url) {
            this.desc_url = desc_url;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public int getError_code() {
            return error_code;
        }

        public void setError_code(int error_code) {
            this.error_code = error_code;
        }

        public int getExpires_in() {
            return expires_in;
        }

        public void setExpires_in(int expires_in) {
            this.expires_in = expires_in;
        }

        public String getRefresh_token() {
            return refresh_token;
        }

        public void setRefresh_token(String refresh_token) {
            this.refresh_token = refresh_token;
        }
    }
}
